#!/bin/sh
# SPDX-License-Identifier: LGPL-2.1+

set -eu
[ -n "${DEBUG:-}" ] && set -x

PASS=0

cleanup() {
    [ "$PASS" = "1" ] || (echo FAIL && exit 1)
}

trap cleanup EXIT HUP INT TERM

LXCFSDIR=${LXCFSDIR:-/var/lib/lxcfs}

if ! mountpoint -q ${LXCFSDIR}; then
    echo "lxcfs isn't mounted on ${LXCFSDIR}"
    exit 1
fi

IS_CGROUP_V2=0
grep -qF 'cgroup cgroup' /proc/1/mountinfo || IS_CGROUP_V2=1

if [ "$IS_CGROUP_V2" = "1" ]; then
    echo "==> Setting up cgroup in lxcfs_test_proc"
    [ ! -d /sys/fs/cgroup ] && exit 0

    mempath=/sys/fs/cgroup
    rmdir ${mempath}/lxcfs_test_proc 2>/dev/null || true
    mkdir ${mempath}/lxcfs_test_proc

    memory_limit_file=memory.max
    tasks_file=cgroup.procs
else
    echo "==> Setting up memory cgroup in lxcfs_test_proc"
    [ ! -d /sys/fs/cgroup/memory ] && exit 0

    initmemory=$(awk -F: '/memory/ { print $3 }' /proc/1/cgroup)
    mempath=/sys/fs/cgroup/memory/${initmemory}
    rmdir ${mempath}/lxcfs_test_proc 2>/dev/null || true
    mkdir ${mempath}/lxcfs_test_proc

    memory_limit_file=memory.limit_in_bytes
    tasks_file=tasks
fi

echo 1 > ${mempath}/lxcfs_test_proc/${tasks_file}
echo $((64*1024*1024)) > ${mempath}/lxcfs_test_proc/${memory_limit_file}

# Test meminfo
echo "==> Testing /proc/meminfo"
[ "$(grep "^MemTotal:.*kB$" ${LXCFSDIR}/proc/meminfo)" = "$(grep "^MemTotal:.*kB$"  /proc/meminfo)" ]

PASS=1
